package Client端;

import javax.swing.*;
import java.awt.*;


public class inputFrame1 extends JFrame {
    public final static Font font_20 = new Font("", Font.PLAIN, 20);
    private final JTextField input = new JTextField();
    private final JLabel jLabel = new JLabel();
    public JLabel tip;
    public String senderId;
    private String type;

    public inputFrame1() {
        int width = 300;
        int height = 150;
        setSize(width, height);
        setLocationRelativeTo(null);
        setLayout(null);
        jLabel.setBounds(10, 10, 80, 30);
        jLabel.setFont(font_20);
        input.setBounds(100, 10, 180, 30);
        input.setFont(font_20);
        tip = new JLabel("", JLabel.CENTER);
        tip.setBounds(40, 50, 200, 20);
        tip.setFont(font_20);
        add(tip);
        add(jLabel);
        add(input);
        JButton submit = new JButton("提交");
        submit.setBounds(30, 80, 100, 30);
        submit.setFont(font_20);
        JButton reset = new JButton("重置");
        reset.setBounds(160, 80, 100, 30);
        reset.setFont(font_20);
        add(submit);
        add(reset);
        reset.addActionListener(e -> input.setText(""));
        submit.addActionListener(e -> {
            String receiverId = input.getText();
            if (receiverId == null) tip.setText(type + "不能为空");
            else if ("好友号".equals(type)) {//好友号处理
                if (receiverId.length() != 8) tip.setText("好友号长度为8位");
                else if (senderId.equals(receiverId)) tip.setText("好友号不能为自己");
                else if (ClientDeal.addFriend(senderId, receiverId)) {
                    Object.clientFrame.renewFriends();
                    setVisible(false);
                } else tip.setText("账号不存在或已经是朋友");
            } else if ("群聊号".equals(type)) {
                if (receiverId.length() != 9) tip.setText("群聊号长度为9位");
                else if (ClientDeal.addGroup(senderId, receiverId)) {
                    Object.clientFrame.renewGroups();
                    setVisible(false);
                } else tip.setText("账号已加入该群聊或群聊不存在");
            } else tip.setText("type错误");
        });
    }

    public void wake(String name, String senderId) {
        type = name;
        jLabel.setText(name + ": ");
        input.setText("");
        tip.setText("");
        this.senderId = senderId;
        setVisible(true);
    }
}
